﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Security_User_Detail : DetailPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.initializeWebForm();

        if (!IsPostBack)
        {
            Guid _userGuid = Guid.Empty;

            if (Request.QueryString["UserGuid"] != null)
            {
                _userGuid = new Guid(Request.QueryString["UserGuid"]);
            }

            if (_userGuid != Guid.Empty)
            {
                Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

                System.Data.DataTable userTable = securityProvider.UserGetByUserGuid(_userGuid);

                if (userTable.Rows.Count == 1)
                {
                    System.Data.DataRow userRow = userTable.Rows[0];

                    userIdTextBox.Text = System.Convert.ToString(userRow["UserId"]);
                    userGuidTextBox.Text = System.Convert.ToString(userRow["UserGuid"]);

                    usernameTextBox.Text = System.Convert.ToString(userRow["Username"]);

                    passwordTextBox.Text = System.Convert.ToString(userRow["Password"]);
                    confirmPasswordTextBox.Text = passwordTextBox.Text;

                    emailTextBox.Text = System.Convert.ToString(userRow["email"]);
                    confirmEmailTextBox.Text = emailTextBox.Text;

                    activeCheckBox.Checked = System.Convert.ToBoolean(userRow["Active"]);
                    startupScriptTextBox.Text = System.Convert.ToString(userRow["StartupScript"]);
                }

                System.Data.DataTable roleTable = securityProvider.RoleGetList();

                groupDropDownList.DataSource = roleTable;
                groupDropDownList.DataTextField = "RoleName";
                groupDropDownList.DataValueField = "RoleGuid";

                groupDropDownList.DataBind();

                System.Data.DataTable userRoleTable = securityProvider.RoleGetByUserGuid(_userGuid);

                groupListBox.DataSource = userRoleTable;
                groupListBox.DataTextField = "RoleName";
                groupListBox.DataValueField = "RoleGuid";

                groupListBox.DataBind();
            }

            switch (formMode)
            {
                case FormMode.Create:
                    usernameTextBox.Enabled = true;
                    passwordTextBox.Enabled = true;
                    confirmPasswordTextBox.Enabled = true;
                    emailTextBox.Enabled = true;
                    confirmEmailTextBox.Enabled = true;
                    activeCheckBox.Enabled = true;
                    startupScriptTextBox.Enabled = true;
                    groupDropDownList.Enabled = true;

                    usernameTextBox.ReadOnly = false;
                    passwordTextBox.ReadOnly = false;
                    confirmPasswordTextBox.ReadOnly = false;
                    emailTextBox.ReadOnly = false;
                    confirmEmailTextBox.ReadOnly = false;
                    startupScriptTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Delete:
                    usernameTextBox.Enabled = true;
                    passwordTextBox.Enabled = true;
                    confirmPasswordTextBox.Enabled = true;
                    emailTextBox.Enabled = true;
                    confirmEmailTextBox.Enabled = true;
                    activeCheckBox.Enabled = true;
                    startupScriptTextBox.Enabled = true;
                    groupDropDownList.Enabled = true;

                    usernameTextBox.ReadOnly = true;
                    passwordTextBox.ReadOnly = true;
                    confirmPasswordTextBox.ReadOnly = true;
                    emailTextBox.ReadOnly = true;
                    confirmEmailTextBox.ReadOnly = true;
                    startupScriptTextBox.ReadOnly = true;

                    deleteImageButton.Visible = true;
                    break;
                case FormMode.Edit:
                    usernameTextBox.Enabled = true;
                    passwordTextBox.Enabled = true;
                    confirmPasswordTextBox.Enabled = true;
                    emailTextBox.Enabled = true;
                    confirmEmailTextBox.Enabled = true;
                    activeCheckBox.Enabled = true;
                    startupScriptTextBox.Enabled = true;
                    groupDropDownList.Enabled = true;

                    usernameTextBox.ReadOnly = false;
                    passwordTextBox.ReadOnly = false;
                    confirmPasswordTextBox.ReadOnly = false;
                    emailTextBox.ReadOnly = false;
                    confirmEmailTextBox.ReadOnly = false;
                    startupScriptTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Default:
                    usernameTextBox.Enabled = true;
                    passwordTextBox.Enabled = true;
                    confirmPasswordTextBox.Enabled = true;
                    emailTextBox.Enabled = true;
                    confirmEmailTextBox.Enabled = true;
                    activeCheckBox.Enabled = true;
                    startupScriptTextBox.Enabled = true;
                    groupDropDownList.Enabled = true;

                    usernameTextBox.ReadOnly = true;
                    passwordTextBox.ReadOnly = true;
                    confirmPasswordTextBox.ReadOnly = true;
                    emailTextBox.ReadOnly = true;
                    confirmEmailTextBox.ReadOnly = true;
                    startupScriptTextBox.ReadOnly = true;

                    deleteImageButton.Visible = false;
                    break;
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(this.buildForwardUrl("Default.aspx"));
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

        if (formMode == FormMode.Edit)
        {
            if (!securityProvider.UserUpdate(new Guid(userGuidTextBox.Text),
                System.Convert.ToInt32(userIdTextBox.Text),
                usernameTextBox.Text,
                passwordTextBox.Text,
                emailTextBox.Text,
                activeCheckBox.Checked,
                startupScriptTextBox.Text))
            {
                Response.Write("alert(\"An error occured while updating the database\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
        else if (formMode == FormMode.Create)
        {
            if (securityProvider.UserInsert(
                Guid.NewGuid(),
                usernameTextBox.Text,
                passwordTextBox.Text,
                emailTextBox.Text,
                activeCheckBox.Checked,
                startupScriptTextBox.Text) == 0)
            {
                Response.Write("alert(\"An error occured while creating database entry\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
    }
    protected void groupListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        UnsubscribeButton.Enabled = groupListBox.SelectedIndex != -1;
    }
    protected void subscribeButton_Click(object sender, EventArgs e)
    {
        Guid _userGuid = Guid.Empty;

        if (Request.QueryString["UserGuid"] != null)
        {
            _userGuid = new Guid(Request.QueryString["UserGuid"]);
        }

        if (_userGuid != Guid.Empty)
        {
            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            securityProvider.UserRoleInsert(_userGuid, new Guid(groupDropDownList.SelectedValue));

            System.Data.DataTable userRoleTable = securityProvider.RoleGetByUserGuid(_userGuid);

            groupListBox.DataSource = userRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();
        }
    }
    protected void UnsubscribeButton_Click(object sender, EventArgs e)
    {
        Guid _userGuid = Guid.Empty;

        if (Request.QueryString["UserGuid"] != null)
        {
            _userGuid = new Guid(Request.QueryString["UserGuid"]);
        }

        if (_userGuid != Guid.Empty)
        {
            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            securityProvider.UserRoleDelete(_userGuid, new Guid(groupListBox.SelectedValue));

            System.Data.DataTable userRoleTable = securityProvider.RoleGetByUserGuid(_userGuid);

            groupListBox.DataSource = userRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();

            UnsubscribeButton.Enabled = false;
        }
    }
}
